home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume3 / mail-s < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  6.6 KB

  1. Path: xanth!mcnc!gatech!bloom-beacon!tut.cis.ohio-state.edu!husc6!necntc!ncoast!allbery
  2. From: jv@mh.nl.UUCP (Johan Vromans)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i015: mail xmit with subject and suppression
  5. Keywords: mail daemon
  6. Message-ID: <1862@mhres.mh.nl>
  7. Date: 16 May 88 14:36:57 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Reply-To: jv@mh.nl.UUCP (Johan Vromans)
  10. Organization: Multihouse NV, the Netherlands
  11. Lines: 232
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. comp.sources.misc: Volume 3, Issue 15
  15. Submitted-By: "Johan Vromans" <jv@mh.nl.UUCP>
  16. Archive-Name: mail-s
  17.  
  18. This is a trivial program.
  19.  
  20. It reads standard input, and passes it to a mail program only if there are
  21. more than a specified number of lines in the message to be sent.
  22.  
  23. In the default case, empty mail messages are suppressed.
  24.  
  25. Optionally, a subject can be passed to the program, which will be added to
  26. the message.
  27.  
  28. It's main purpose is to cut down the number of mails sent to the system
  29. manager from system daemons - only if there is something important to tell.
  30.  
  31. There are a few examples in the manual page.
  32.  
  33. #!/bin/sh
  34. # This is a shell archive, meaning:
  35. # 1. Remove everything above the #! /bin/sh line.
  36. # 2. Save the resulting text in a file.
  37. # 3. Execute the file with /bin/sh (not csh) to create:
  38. #    README
  39. #    mail-s.1
  40. #    mail-s.c
  41. # This archive created: Mon May 16 16:34:17 1988
  42. export PATH; PATH=/bin:/usr/bin:$PATH
  43. if test -f 'README'
  44. then
  45.     echo shar: "will not over-write existing file 'README'"
  46. else
  47. sed 's/^X//' << \SHAR_EOF > 'README'
  48. XThis is a trivial program.
  49. X
  50. XIt reads standard input, and passes it to a mail program only if there are
  51. Xmore than a specified number of lines in the message to be sent.
  52. X
  53. XIn the default case, empty mail messages are suppressed.
  54. X
  55. XOptionally, a subject can be passed to the program, which will be added to
  56. Xthe message.
  57. X
  58. XIt's main purpose is to cut down the number of mails sent to the system
  59. Xmanager from system daemons - only if there is something important to tell.
  60. X
  61. XThere are a few examples in the manual page.
  62. X
  63. XNo makefile - just compile and run. The only compile-time option is the
  64. Xname of the mailer to be used. This defaults to "/bin/smail", but can be
  65. Xchanged with
  66. X
  67. X    -DMAILER=\"/usr/local/bin/mymailer\"
  68. X
  69. XPut into public domain by the author on May 16, 1988.
  70. XJohan Vromans, Multihouse Research.
  71. SHAR_EOF
  72. if test 799 -ne "`wc -c < 'README'`"
  73. then
  74.     echo shar: "error transmitting 'README'" '(should have been 799 characters)'
  75. fi
  76. fi
  77. if test -f 'mail-s.1'
  78. then
  79.     echo shar: "will not over-write existing file 'mail-s.1'"
  80. else
  81. sed 's/^X//' << \SHAR_EOF > 'mail-s.1'
  82. X.TH MAIL-S 1 "Contributed"
  83. X.SH SYNOPSIS
  84. X.B mail-s
  85. X.RB "[ " -n
  86. X.IR "threshold" " ]"
  87. X.RB "[ " -s
  88. X.IR "subject" " ]"
  89. X.IR "recipient" " [...]"
  90. X.SH DESCRPITION
  91. X.B Mail-s
  92. Xtakes a mail message on standard input, and passes it to the mailer program
  93. Xonly if the mail message contains more than a specified number of lines.
  94. X.PP
  95. X.SH OPTIONS
  96. X.IP -n\ \fIthreshold\fP
  97. XThe minimum number of lines the message must contain. Default value is 1.
  98. X.IP -s\ \fIsubject\fP
  99. XSupply the given subject to the mail message. Default is no subject.
  100. X.ne 15v
  101. X.SH EXAMPLE
  102. XThis program is intended for use in system maintenance procedures, to
  103. Xminimize the amount of mails sent.
  104. X.sp
  105. X.in +5
  106. Xgrep "[Ee]rror" < logfile \e
  107. X.br
  108. X| mail-s -s "Something wrong" root
  109. X.in
  110. X.sp
  111. XThis command will cause a mail to be sent to the super user if there were
  112. Xlines in the logfile with the text "Error" or "error" in it.
  113. X.sp
  114. X.in +5
  115. Xls -l /applic/queuefile \e
  116. X.br
  117. X| mail-s -n 10 -s "More than 10 entries waiting" root
  118. X.in
  119. X.sp
  120. XOnly if there are 10 or more files in this directory, the super user will
  121. Xbe notified.
  122. X.SH AUTHOR
  123. XJohan Vromans, Multihouse Research <jv@mh.nl>
  124. X.br
  125. XThis program is in the public domain.
  126. X.sp
  127. XComments, bug fixes welcomed - but I don't guarantee to fix anything.
  128. SHAR_EOF
  129. if test 1230 -ne "`wc -c < 'mail-s.1'`"
  130. then
  131.     echo shar: "error transmitting 'mail-s.1'" '(should have been 1230 characters)'
  132. fi
  133. fi
  134. if test -f 'mail-s.c'
  135. then
  136.     echo shar: "will not over-write existing file 'mail-s.c'"
  137. else
  138. sed 's/^X//' << \SHAR_EOF > 'mail-s.c'
  139. X/* mail-s
  140. X *
  141. X *    Send mail (with an optional subject) iff standard input contains
  142. X *    more than a given minimum of lines.
  143. X *
  144. X *    Typical usage from system maintenance scripts:
  145. X *
  146. X *        grep "[Ee]rror" < logfile | mail-s "Something wrong" root
  147. X *
  148. X */
  149. X
  150. Xstatic char SCCS_id[] = "@(#)@ mail-s    1.2 - mail-s";
  151. Xstatic char C_id[] = "@(#)@ Copyright 1988 Johan Vromans, Multihouse Research";
  152. X
  153. X#include <stdio.h>
  154. X#include <ctype.h>
  155. X
  156. X#ifndef MAILER
  157. X#define MAILER    "/bin/smail"
  158. X#endif
  159. X
  160. Xmain (argc, argv)
  161. Xint argc;
  162. Xchar *argv[];
  163. X{
  164. X    int c;
  165. X    int errflg        = 0;        /* errors seen */
  166. X    int f_thresh    = 1;        /* threshold value */
  167. X    char *f_subj    = NULL;        /* subject line */
  168. X    char buf [BUFSIZ];            /* file copy buffer */
  169. X    FILE *tmp;                /* workfile */
  170. X    extern char *optarg;        /* getopt interface */
  171. X    extern int optind;            /* getopt interface */
  172. X
  173. X    /* Phase one: command line options parsing */
  174. X
  175. X    while ((c = getopt (argc, argv, "s:n:")) != EOF) {
  176. X    switch (c) {
  177. X    case 'n':
  178. X        /* threshold: xmit to mailer iff stdin contains at least
  179. X         * this much lines.
  180. X         */
  181. X        f_thresh = atoi (optarg);
  182. X        if (!isdigit(*optarg)) errflg++;
  183. X        break;
  184. X    case 's':
  185. X        /* subject to pass to mailer */
  186. X        f_subj = optarg;
  187. X        break;
  188. X    default:
  189. X        errflg++;
  190. X        break;
  191. X    }
  192. X    }
  193. X
  194. X    if (errflg) {
  195. X    fprintf (stderr,
  196. X        "Usage mail-s [-s subject] [-n threshold] recipients\n");
  197. X    exit (2);
  198. X    }
  199. X
  200. X    /* phase two: copy the file and decrease f_thresh upon each complete
  201. X     * line. The work file is passed to the mailer, so put in the
  202. X     * subject first.
  203. X     */
  204. X
  205. X    tmp = tmpfile ();
  206. X
  207. X    if (f_subj)
  208. X    fprintf (tmp, "Subject: %s\n\n", f_subj);
  209. X
  210. X    while (fgets (buf, BUFSIZ, stdin) != NULL) {
  211. X    register int len = strlen (buf);
  212. X    fputs (buf, tmp);
  213. X    if (len >= 1 && buf[len-1] == '\n') f_thresh--;
  214. X    }
  215. X
  216. X    /* Check for go or no-go */
  217. X    if (f_thresh > 0)        /* didn't make it */
  218. X    return 0;
  219. X
  220. X    /* Phase three: rewind the workfile, attach to standard input,
  221. X     * build new arg vector, and exec the mailer.
  222. X     */
  223. X
  224. X    rewind (tmp);
  225. X    close (0);            /* stdin */
  226. X    dup (fileno (tmp));        /* attach to standard input */
  227. X
  228. X    argv[0] = "rmail";        /* build rmail arg vector */
  229. X    for (c = 1; optind < argc; optind++, c++)
  230. X    argv[c] = argv[optind];
  231. X    argv[c] = NULL;
  232. X    return execv (MAILER, argv);    /* and Go */
  233. X}
  234. X
  235. SHAR_EOF
  236. if test 2310 -ne "`wc -c < 'mail-s.c'`"
  237. then
  238.     echo shar: "error transmitting 'mail-s.c'" '(should have been 2310 characters)'
  239. fi
  240. fi
  241. exit 0
  242. #    End of shell archive
  243.  
  244. -- 
  245. postmaster on node mhres (currently: Johan Vromans)
  246.